# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
import os
from conan import ConanFile
from conan.tools.layout import basic_layout
from conan.tools.env import VirtualBuildEnv
from conan.tools.scm import Git
from conan.tools.files import copy, chdir

required_conan_version = ">=2.13.0"


class OpenSSLConan(ConanFile):
    name = "openssl"
    license = "Apache License 2.0"
    description = "OpenSSL"
    settings = ["os", "compiler", "build_type", "arch"]
    exports_sources = ["export_files/*", "cmake/*", "script/*", "permissions.ini"]

    def layout(self):
        basic_layout(self)
        
    def source(self):
        source = self.conan_data["sources"][self.version]
        git = Git(self)
        git.fetch_commit(url=source["url"], commit=source["branch"])

    def build(self):
        venv = VirtualBuildEnv(self)
        venv.environment().append("CFLAGS", "-Wno-unused-parameter")
        venv.environment().define("NO_OSSL_RENAME_VERSION", "1")
        venv.generate()

        # 回退到上一级目录，不适用conan2 generate的目录
        os.chdir("..")
        if self.settings.arch == "armv8":
            self.run(f"./Configure --prefix={self.package_folder}/usr \
                --openssldir={self.package_folder}/usr/lib64/ssl-3 \
                --libdir={self.package_folder}/usr/lib64/spdm_ssl linux-aarch64 \
                no-afalgeng -D_FORTIFY_SOURCE=2 -O2 -fPIC -fstack-protector-strong \
                -shared -Wl,-z,relro,-z,noexecstack,-z,now -rdynamic")
        elif self.settings.arch == "x86_64":
            self.run(f"./Configure --prefix={self.package_folder}/usr \
                --openssldir={self.package_folder}/usr/lib64/ssl-3 \
                --libdir={self.package_folder}/usr/lib64/spdm_ssl linux-x86_64 \
                no-afalgeng -D_FORTIFY_SOURCE=2 -O2 -fPIC -fstack-protector-strong \
                -shared -Wl,-z,relro,-z,noexecstack,-z,now -rdynamic")
        self.run("make -j32")

    def generate(self):
        pass

    def package(self):
        copy(self, "*", src=self.source_folder + "/include", dst=self.package_folder + "/include/spdm_ssl")
        copy(self, "libssl.so.3", src=self.build_folder + "/..", dst=self.package_folder + "/usr/lib64/spdm_ssl")
        copy(self, "libcrypto.so.3", src=self.build_folder + "/..", dst=self.package_folder + "/usr/lib64/spdm_ssl")

        lib_path = os.path.join(self.package_folder, "usr/lib64/spdm_ssl")
        with chdir(self, lib_path):
            self.run(f"ln -sf libssl.so.3 libssl.so")
            self.run(f"ln -sf libcrypto.so.3 libcrypto.so")

    def package_info(self):
        self.cpp_info.libdirs = ["usr/lib64/spdm_ssl"]
        self.cpp_info.includedirs = ["include/spdm_ssl"]
        self.cpp_info.libs = ["crypto", "ssl"]